// Copyright. All rights reserved.

/*
Package util ....
*/
package util

import (
    "strconv"
)

func parseInt(a string, n int) int {
    r, _ := strconv.ParseInt(a, n, 32)
    return int(r)
}

var B64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
func charAt(pos int) string {
    return string(B64map[pos])
}

func Hex2b64(a string) string {
    b64padchar := "="
    b, c := 0, 0
    d := "";
    for b = 0; b + 3 <= len(a); b += 3 {
        c = parseInt(a[b:b + 3], 16)
        d += charAt(c >> 6) + charAt(63 & c)
    }

    if b + 1 == len(a) {
        c = parseInt(a[b:b+1], 16)
        d += charAt(c << 2)
    } else {
        if b + 2 == len(a) {
            c = parseInt(a[b:b + 2], 16)
            d += charAt(c >> 2) + charAt((3 & c) << 4)
        }
    }

    if (3 & len(d)) > 0 {
        d += b64padchar
    }

    return d
}
